package net.huashitong.node.utile;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
	
	/**
	 * 求x天后(前)的时间
	 * @param
	 * @param day 天(+为x天后,-为x天前)
	 * @return 计算后的时间
	 * @throws ParseException 
	 */
	public static String addDays(String dateStr, int day) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
			dateStr=dateStr.replace("-", ".");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(dateStr));
			cal.add(Calendar.DATE, day);
			return sdf.format(cal.getTime());
		} catch (Exception e) {
			return null;
		}
		
	}
	/**
	* 通过时间秒毫秒数判断两个时间的间隔
	* @param date1
	* @param date2
	* @return
	*/
	public static int betweenTwoDateNum(String date1,String date2){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
			int days = (int) ((sdf.parse(date2).getTime() - sdf.parse(date1).getTime()) / (1000*3600*24));
			return days;
		} catch (Exception e) {
			return 0;
		}
		
	}

	/**
	 * 判断是否周末
	 * @param date
	 * @return
	 * @throws ParseException
	 */
    public static boolean isWEEK(Date date) throws ParseException {
    	Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY) {
			return true;
		}
		return  false;
	}

	/**
	 * 获取两个日期之间的日期
	 * @param start
	 * @param end
	 * @return
	 */
    public static List<Date> getBetweenDates(Date start, Date end){
		// 返回的日期集合
		List<Date> days = new ArrayList<>();
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(start);
		Calendar tempEnd = Calendar.getInstance();
		tempEnd.setTime(end);
		tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
		while (tempStart.before(tempEnd)) {
			days.add(tempStart.getTime());
			tempStart.add(Calendar.DAY_OF_YEAR, 1);
		}
		return days;
	}
	public static int  getStatistics(List<Date> date) throws ParseException{
       int zong=0;
		for(Date da:date){
			if(!isWEEK(da)){
              zong++;
			}
		}
       return zong;
	}
	/**
	* 求当前日期
	* @return
	*/
	public static String getNowDate(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		return df.format(new Date());
	}
	public static String getDate(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");//设置日期格式
		return df.format(new Date());
	}
	public static String getDateByMD(){
		SimpleDateFormat df = new SimpleDateFormat("MM月dd日");//设置日期格式
		return df.format(new Date());
	}
	public static String getDateByYMD(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");//设置日期格式
		return df.format(new Date());
	}


	/**
	 * 计算两个日期之间相差的天数
	 * @param smdate 较小的时间
	 * @param bdate  较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate,Date bdate) throws ParseException
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		smdate=sdf.parse(sdf.format(smdate));
		bdate=sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 *字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate,String bdate) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	public static void main(String[] args) throws ParseException {
		//System.out.println(addDays("2017.11.14", 2));
		//System.out.println(betweenTwoDateNum("2017.11.15", "2017.11.16"));
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		String bDate = "2018-10-20";
		String aDate="2018-10-30";
		Date bdate = format1.parse(bDate);
		Date adate = format1.parse(aDate);
		DateUtil du=new DateUtil();
		List<Date> list=du.getBetweenDates(bdate,adate);
		int i=du.getStatistics(list);
		System.out.println("i="+i);


	}
	

}
